package com.net.xpay.core.validator;

import com.google.common.base.Strings;
import com.net.xpay.common.enums.invoice.OpenInvoiceStatus;
import com.net.common.exception.BusinessException;
import com.net.common.util.MoneyUtil;
import com.net.common.util.PhoneUtil;
import com.net.common.util.SerializationUtil;
import com.net.xpay.core.constant.PoseidonErrorCode;
import com.net.xpay.core.domain.ChargeRecord;
import com.net.xpay.core.enums.PayStatus;
import com.net.xpay.core.manager.ChargeRecordManager;
import com.net.xpay.core.model.InvoiceModel;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

/**发票验证
 * @author 86188
 */
@Component
@Slf4j
public class InvoiceValidator {

    @Autowired
    private ChargeRecordManager chargeRecordManager;

    /**
     * 电子发票验证
     * @param model
     */
    public void validateSpecialInvoice(InvoiceModel model) {
        log.info("validateInvoiceTitle model={}", SerializationUtil.obj2String(model));
        if (model.getInvoiceTitleType() == null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "抬头类型不能为空");
        }
        if (model.getInvoiceType() == null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "发票类型不能为空");
        }
        if (Strings.isNullOrEmpty(model.getInvoiceTitle())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "发票抬头不能为空");
        }
        if (Strings.isNullOrEmpty(model.getTaxIdNumber())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "纳税人识别号不能为空");
        }
        //
        if (model.getInvoiceAmount().compareTo(BigDecimal.ZERO) == 0) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "开票金额不能为空");
        }

        checkCommonInvoice(model);
    }

    /**
     * 纸质发票验证
     * @param model
     */
    public void validateSpecialPaperInvoice(InvoiceModel model) {
        log.info("validateInvoiceTitle model={}", SerializationUtil.obj2String(model));
        if (model.getInvoiceTitleType() == null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "抬头类型不能为空");
        }
        if (model.getInvoiceType() == null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "发票类型不能为空");
        }
        if (Strings.isNullOrEmpty(model.getInvoiceTitle())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "发票抬头不能为空");
        }
        if (Strings.isNullOrEmpty(model.getTaxIdNumber())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "纳税人识别号不能为空");
        }
        if (Strings.isNullOrEmpty(model.getAccountsBank())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "基本开户银行不能为空");
        }
        if (Strings.isNullOrEmpty(model.getAccountNo())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "基本开户账号不能为空");
        }

        //邮寄地址
        if (Strings.isNullOrEmpty(model.getRegisteredAddress())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "企业注册地址不能为空");
        }
        if (Strings.isNullOrEmpty(model.getRegisteredPhone())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "企业注册电话不能为空");
        }
        if (!PhoneUtil.isEffectiveMobile(model.getRegisteredPhone())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "手机号格式不正确");
        }
        if (Strings.isNullOrEmpty(model.getRecipientName())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "收件人姓名不能为空");
        }
        if (Strings.isNullOrEmpty(model.getProvince())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "省份不能为空，请选择省份");
        }
        if (Strings.isNullOrEmpty(model.getCity())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "城市不能为空，请选择城市");
        }
        if (Strings.isNullOrEmpty(model.getDistrict())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "县区不能为空，请选择县区");
        }
        if (Strings.isNullOrEmpty(model.getDetailedAddress())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "详细地址不能为空");
        }
        if (Strings.isNullOrEmpty(model.getContactsPhone())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "联系人手机号不能为空");
        }
        if (!PhoneUtil.isEffectiveMobile(model.getContactsPhone())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "手机号格式不正确");
        }
        //
        if (model.getInvoiceAmount().compareTo(BigDecimal.ZERO) == 0) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "开票金额不能为空");
        }

        checkCommonInvoice(model);
    }

    //验证公有的逻辑
    public void checkCommonInvoice(InvoiceModel model) {
        List<Long> ids = model.getChargeRecordIds();
        if (CollectionUtils.isEmpty(ids)) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "未有充值记录,开票失败");
        }
        List<ChargeRecord> chargeRecords = chargeRecordManager.listByIds(ids);
        BigDecimal amount = new BigDecimal("0");
        for (ChargeRecord record : chargeRecords) {
            if (record.getPayStatus() != PayStatus.PAYED) {
                throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "充值记录未付款");
            }
            if (record.getOpenInvoiceStatus() != null && record.getOpenInvoiceStatus() != OpenInvoiceStatus.WAIT) {
                throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "充值记录不是待开票状态");
            }
            amount = amount.add(MoneyUtil.convertCentToBigDecimal(record.getAmount(), 2));
        }
        if (amount.compareTo(model.getInvoiceAmount()) == 0) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "开票金额错误");
        }
    }
}
